package bullsora.kindlerss.controllers;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Sorin Buliarca
 */
@Controller
public class SendIpController {

  public static final Log LOG = LogFactory.getLog(SendIpController.class);

  @RequestMapping(value = "/sendIp/*")
  public String sendIp(@RequestParam(value = "ip") String ip) {
    LOG.info("Sending new ip " + ip);
    MailService mailService = MailServiceFactory.getMailService();

    try {
      MailService.Message msg = new MailService.Message();
      msg.setSender("bullsora@gmail.com");
      msg.setTo("bullsora@gmail.com");
      msg.setSubject("New ip :" + ip);
      msg.setTextBody("");
      mailService.send(msg);
    } catch (Exception e) {
      LOG.error("Error while sending mail", e);
    }
    return "empty";
  }
}
